package com.itheima.studentproject.controller;


import com.itheima.studentproject.domain.Student;
import com.itheima.studentproject.service.StudentService;

import java.util.Scanner;

public class StudentController {


    public void start(){
        Scanner sc = new Scanner(System.in);
        loop:while (true){
            System.out.println("==============欢迎来到学生管理系统==============");
            System.out.println("1.添加学生");
            System.out.println("2.修改学生");
            System.out.println("3.删除学生");
            System.out.println("4.查看学生");
            System.out.println("5.退出");
            String str = sc.next();

            switch (str){
                case "1":
                    addStudent();
                    break;
                case "2":
                    updateStudent();
                    break;
                case "3":
                    deleteStudent();
                    break;
                case "4":
                    findStudent();
                    break;
                case "5":
                    System.out.println("谢谢您的使用");
                    break loop;
                default:
                    System.out.println("您的输入有误");
                    break;
            }
        }
    }

    private void deleteStudent() {
        Scanner sc = new Scanner(System.in);

        System.out.println("请输入学号：");
        String id = sc.next();
        StudentService studentService = new StudentService();
        int index = studentService.isExits(id);
        if(index == -1){
            System.out.println("学号不存在！");
            return;
        }

        boolean flag = studentService.deleteStudent(index);

        if(flag){
            System.out.println("删除成功！");
        }else{
            System.out.println("删除失败");
        }
    }

    private void updateStudent() {
        Scanner sc = new Scanner(System.in);

        System.out.println("请输入学号：");
        String id = sc.next();
        StudentService studentService = new StudentService();
        int index = studentService.isExits(id);
        if(index == -1){
            System.out.println("学号不存在！");
            return;
        }
        System.out.println("请输入学生姓名");
        String name = sc.next();
        System.out.println("请输入学生年龄");
        String age = sc.next();
        System.out.println("请输入学生生日");
        String birthday = sc.next();

        Student stu = new Student(id,name,age,birthday);
        boolean flag = studentService.updateStudent(stu);

        if(flag){
            System.out.println("修改成功！");
        }else{
            System.out.println("修改失败");
        }

    }

    private void findStudent() {
        StudentService studentService = new StudentService();
        Student[] arr = studentService.findStudent();
        if(arr == null){
            System.out.println("当前没有学生信息，请您输入后再试！");
            return;
        }
        System.out.println("学号----姓名----年龄----生日");
        for (int i = 0; i < arr.length; i++) {
            Student stu = arr[i];
            if(stu != null){
                System.out.println(stu.getId()+
                        "\t"+stu.getName()+
                        "\t"+stu.getAge()+
                        "\t"+stu.getBirthday());
            }
        }

    }

    private void addStudent(){
        Scanner sc = new Scanner(System.in);
        StudentService studentService = new StudentService();
        System.out.println("请输入学生id");
        String id;
        while (true){
            id = sc.next();
            int index = studentService.isExits(id);
            if(index == -1){
                break;
            }
            System.out.println("重复学生id！");
        }
        System.out.println("请输入学生姓名");
        String name = sc.next();
        System.out.println("请输入学生年龄");
        String age = sc.next();
        System.out.println("请输入学生生日");
        String birthday = sc.next();

        Student stu = new Student(id,name,age,birthday);

        boolean flag = studentService.addStudent(stu);

        if(flag){
            System.out.println("添加成功");
        }else{
            System.out.println("添加失败");
        }

    }
}
